//
//  SelectionNumManager.m
//  CaiPiao
//
//  Created by apple on 2018/5/19.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "SelectionNumManager.h"

@implementation SelectionNumManager

+(SelectionNumManager*)shared{
    static SelectionNumManager* snManager;
    static dispatch_once_t slOnceT;
    dispatch_once(&slOnceT, ^{
        snManager = [[SelectionNumManager alloc] init];
    });
    return snManager;
}

#pragma mark - 网络请求
/***
 actionID:
 203:显示遗漏
 201：最近开奖日期
 202：近期开奖
 lotteryID：
 ***/
-(void)getDataWithLotteryID:(NSString*)lotteryID andActionID:(NSString*)actionID andCallBack:(void(^)(BOOL load,NSDictionary* resObj))callBack{
    NSString* paramsString = [NSString stringWithFormat:@"{\"LotteryID\":\"%@\"}",lotteryID];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]init];
    [manager GET:@"https://data.cp.lovedou.com/Handler.ashx" parameters:@{@"action":actionID,@"params":paramsString} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",task.response.URL);
        
        if (callBack) {
            callBack(YES,responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (callBack) {
            callBack(NO,nil);
        }
    }];
}


#pragma mark - 获取红球总个数
-(NSArray*)redNumberArrayWith:(NSString*)lotteryID{
    NSArray* numbersArray = @[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",
                              @"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",
                              @"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",
                              @"31",@"32",@"33",@"34",@"35",@"36",@"37",@"38",@"39",@"40"];
    
    NSInteger ExtractionArrayLength = 0;
    NSMutableArray* ExtractionArray = [NSMutableArray arrayWithCapacity:0];
    switch ([lotteryID integerValue]) {
        case 3:
            ExtractionArrayLength = 33;
            
            break;
        case 4:
            ExtractionArrayLength = 35;
            
            break;
    
        case 7:
            ExtractionArrayLength = 11;
            break;
        
            break;
        default:
            break;
    }
    
    for (int i=0; i<ExtractionArrayLength; i++) {
        [ExtractionArray addObject:numbersArray[i]];
    }
    return ExtractionArray;
}

#pragma mark - 获取蓝球总个数
-(NSArray*)blueNumberArrayWith:(NSString*)lotteryID{
    NSArray* numbersArray = @[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",
                              @"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",
                              @"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",
                              @"31",@"32",@"33",@"34",@"35",@"36",@"37",@"38",@"39",@"40",];
    NSMutableArray* ExtractionArray = [NSMutableArray arrayWithCapacity:0];
    NSInteger ExtractionArrayLength = 0;
    switch ([lotteryID integerValue]) {
        case 3:
            ExtractionArrayLength = 16;
            break;
        case 4:
            ExtractionArrayLength = 12;
            break;
        default:
            break;
    }
    
    for (int i=0; i<ExtractionArrayLength; i++) {
        [ExtractionArray addObject:numbersArray[i]];
    }
    return ExtractionArray;
}

#pragma mark - 获取随机一组红球
-(NSArray*)randomRedNumberActionWithLottoryID:(NSString*)lotteryID{

    NSMutableArray* ExtractionArray = [NSMutableArray arrayWithCapacity:0];
    [ExtractionArray addObjectsFromArray:[self redNumberArrayWith:lotteryID]];
    NSInteger ExtractionNumberCount = 0;
    switch ([lotteryID integerValue]) {
        case 3:
            ExtractionNumberCount = 6;
            break;
        case 4:
            ExtractionNumberCount = 5;
            break;
        case 5:{
            ExtractionNumberCount = 3;
            NSString* Number1 = [NSString stringWithFormat:@"%d",arc4random()%10];
            NSString* Number2 = [NSString stringWithFormat:@"%d",arc4random()%10];
            NSString* Number3 = [NSString stringWithFormat:@"%d",arc4random()%10];
            return @[Number1,Number2,Number3];
        }
            break;
        case 6:{
            ExtractionNumberCount = 3;
            NSString* Number1 = [NSString stringWithFormat:@"%d",arc4random()%10];
            NSString* Number2 = [NSString stringWithFormat:@"%d",arc4random()%10];
            NSString* Number3 = [NSString stringWithFormat:@"%d",arc4random()%10];
            return @[Number1,Number2,Number3];
        }
            break;
        case 7:
            ExtractionNumberCount = 5;
            break;
        case 9:{
            ExtractionNumberCount = 3;
            NSString* Number1 = [NSString stringWithFormat:@"%d",arc4random()%10];
            NSString* Number2 = [NSString stringWithFormat:@"%d",arc4random()%10];
            NSString* Number3 = [NSString stringWithFormat:@"%d",arc4random()%10];
            return @[Number1,Number2,Number3];
        }
            break;
        default:
            break;
    }
    NSMutableArray* callbackArray = [NSMutableArray arrayWithCapacity:0];
    for (int i=0; i<ExtractionNumberCount; i++) {
        NSInteger randomIndex = arc4random()%[ExtractionArray count];
        [callbackArray addObject:[ExtractionArray objectAtIndex:randomIndex]];
        [ExtractionArray removeObjectAtIndex:randomIndex];
    }
    
    return [callbackArray sortedArrayUsingSelector:@selector(compare:)];
}


#pragma mark - 获取随机一组蓝球
-(NSArray*)randomBlueNumberActionWithLottoryID:(NSString*)lotteryID{
    NSMutableArray* ExtractionArray = [NSMutableArray arrayWithCapacity:0];
    [ExtractionArray addObjectsFromArray:[self blueNumberArrayWith:lotteryID]];
    NSInteger ExtractionNumberCount = 0;
    switch ([lotteryID integerValue]) {
        case 3:
            ExtractionNumberCount = 1;
            break;
        case 4:
            ExtractionNumberCount = 2;
            break;
        case 5:
            return @[];
        case 6:
            return @[];
        case 7:
            return @[];
        case 9:
            return @[];
        default:
            break;
    }
    
    NSMutableArray* callbackArray = [NSMutableArray arrayWithCapacity:0];
    for (int i=0; i<ExtractionNumberCount; i++) {
        NSInteger randomIndex = arc4random()%[ExtractionArray count];
        [callbackArray addObject:[ExtractionArray objectAtIndex:randomIndex]];
        [ExtractionArray removeObjectAtIndex:randomIndex];
    }
    return callbackArray;
}

#pragma mark - 计算一共多少注
-(NSInteger)calculationsTheCombinationWith:(NSInteger)arrayCount andExtractionCount:(NSInteger)ExtractionCount{
    NSInteger denominator = 1;//分母
    NSInteger molecular = 1;//分子
    
    for (int i=0; i<arrayCount-ExtractionCount; i++) {
        molecular = molecular*(arrayCount-i);
    }
    for (int i=0; i<arrayCount-ExtractionCount; i++) {
        denominator = denominator*((arrayCount-ExtractionCount)-i);
    }
    return molecular/denominator;
}

@end
